/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.listener.processor;

import org.apache.axiom.om.*;
import org.apache.log4j.*;
import org.apache.axiom.soap.*;
import org.apache.axis2.addressing.*;
import javax.xml.namespace.QName;
import lk.mrt.cse.wihidum.client.*;
import lk.mrt.cse.wihidum.util.*;
import lk.mrt.cse.wihidum.listener.storage.*;

public class SinkSubscriptionProcessor
{
    private static final Logger logger = Logger.getLogger (SinkSubscriptionProcessor.class);
   
    public void processSubscriptionResponseMessage(SOAPEnvelope response)
    {
             logger.info("Processing Subscription Response Message");

             SOAPHeader responseHeader=response.getHeader();
             SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
             OMNamespace wihidumNS = factory.createOMNamespace(WSEventingConstants.WIHIDUM_NAMESPACE, WSEventingConstants.WHIDUM_NAMESPACE_PREFIX);
             OMElement mySubscriptionElement=responseHeader.getFirstChildWithName(new QName(WSEventingConstants.WIHIDUM_NAMESPACE,WSEventingConstants.ElementNames.SubscriptionID));
             String subscriptionID=mySubscriptionElement.getText();

             OMElement filterElement=responseHeader.getFirstChildWithName(new QName(WSEventingConstants.WIHIDUM_NAMESPACE,WSEventingConstants.ElementNames.Filter));
             String filter=filterElement.getText();

             SOAPBody responseBody=response.getBody();
             OMElement responseElement=responseBody.getFirstChildWithName(new QName(WSEventingConstants.EVENTING_NAMESPACE,WSEventingConstants.ElementNames.SubscribeResponse));
             OMElement subManagerElement=responseElement.getFirstChildWithName(new QName(WSEventingConstants.EVENTING_NAMESPACE,WSEventingConstants.ElementNames.SubscriptionManager));
             OMElement subManagerAddressElement=subManagerElement.getFirstChildWithName(new QName(WSEventingConstants.ADDRESSING_NAMESPACE,WSEventingConstants.ElementNames.Address));
             String subManagerAddress=subManagerAddressElement.getText();

             OMElement referenceParametersElement=subManagerElement.getFirstChildWithName(new QName(WSEventingConstants.ADDRESSING_NAMESPACE,WSEventingConstants.ElementNames.ReferenceParameters));
             OMElement identifierElement=referenceParametersElement.getFirstChildWithName(new QName(WSEventingConstants.EVENTING_NAMESPACE,WSEventingConstants.ElementNames.Identifier));
             String subscriberUUID=identifierElement.getText();

             Subscription subscription=new Subscription();
             subscription.setUuid(subscriberUUID);
             subscription.setSubscriptionID(subscriptionID);
             subscription.setFilter(filter);
             subscription.setSubscriptionManagerAdress(subManagerAddress);

             logger.info("Saving the subscription details received in the subscription response.");

             SubscriptionDAO subscriptionDAO=new SubscriptionDAO();
             subscriptionDAO.saveSubscriber(subscription);
    }
    public OMElement processUnsubscriptionRequest(SOAPEnvelope unsubscriptionRequest)
    {
           SOAPFactory factory=OMAbstractFactory.getSOAP12Factory();

           SOAPBody unsubscriptionBody=unsubscriptionRequest.getBody();
           OMElement messageElement=unsubscriptionBody.getFirstChildWithName(new QName("UnsubscriptionRequest"));
           OMElement subscriptionID=messageElement.getFirstChildWithName(new QName("subscriptionID"));
           //OMElement subscriptionID=unsubscriptionBody.getFirstChildWithName(new QName("subscriptionID"));
           String subID=subscriptionID.getText();

           OMElement topicElement=messageElement.getFirstChildWithName(new QName("topic"));
           String topic=topicElement.getText();

           SubscriptionDAO subscriptionDAO=new SubscriptionDAO();
           Subscription subscription=subscriptionDAO.getSubscriberDetails(subID,topic);

           UnsubscriptionClientBean unsubscriptionClientBean=new UnsubscriptionClientBean();
           unsubscriptionClientBean.setBrokerAddress(new EndpointReference(subscription.getSubscriptionManagerAdress()));
           logger.info("Subscription has been saved at"+subscription.getSubscriptionManagerAdress());
           unsubscriptionClientBean.setIdentifier(subscription.getUuid());
           logger.info("Subscription unique id"+subscription.getUuid());
           UnsubscriptionClient unsubscriptionClient=new UnsubscriptionClient(unsubscriptionClientBean);
           String response=unsubscriptionClient.unsubscribe();
        
           OMElement responseElement=null;
           if(response.equals("Subscription Manager deleted subscription successfully"))
           {
               subscriptionDAO.deleteSubscription(subscription);
               DeliveryElementDAO deliveryElementDAO=new DeliveryElementDAO();
               deliveryElementDAO.deleteDeliveryElements(subID);
               responseElement=factory.createOMElement("unsubscribe_response",null);
               responseElement.setText("Subscription deleted successfully");
           }

           return responseElement;
    }
}
